"""
Python App Engine + Google Storage.
Based heavily from examples given by Google
Instructions:
just rip the boto lib from the gsutil project, drop it in your python 
App Engine project. 
@author: Adam Thurlow <Thurloat>
"""

import config
import boto
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template

conf = boto.config
conf.add_section('Credentials')
conf.set('Credentials', 'gs_access_key_id', config.STORAGE_KEY)
conf.set('Credentials', 'gs_secret_access_key', config.STORAGE_SECRET)
#FLAGS = ['CREATE', 'LIST_BUCKETS', 'LIST_CONTENTS','DOWNLOAD_FILE']
FLAGS = ['FILE']
DOWNLOAD_PATH = '/path/to/file.ext'
DOWNLOAD_FILENAME = 'file.ext'
BUCKET_NAME = 'chanezon-tweet-sentiment'

class StorageClient():
    """ Client for the Storage service """
    def __init__(self):
        pass

    def create_object(self, filename, content):
        dst_uri = boto.storage_uri(
            BUCKET_NAME + '/' + filename, 'gs')
        dst_uri.new_key().set_contents_from_string(content)
        return dst_uri
        #srow('Successfully created "%s/%s"' % (
        #            dst_uri.bucket_name, dst_uri.object_name))

class DownloadHandler(webapp.RequestHandler):
    def get(self):
        srow = self.response.out.write
        if 'DOWNLOAD_FILE' in FLAGS:
            uri = boto.storage_uri("%s/%s" % (BUCKET_NAME, DOWNLOAD_PATH), 'gs')
            self.response.headers['Content-Type'] = 'application/octet-stream'
            self.response.headers.add_header('content-disposition',
                'attachment',
                filename = DOWNLOAD_FILENAME)
            srow(uri.get_contents_as_string())
            
class MainHandler(webapp.RequestHandler):
    def get(self):
        srow = self.response.out.write
        self.response.out.write("<h4> Testing the Google Storage API </h4>")
        if 'CREATE' in FLAGS:
            srow("<h2>Creating Bucket</h2>")
            uri = boto.storage_uri(BUCKET_NAME, 'gs')
            uri.create_bucket()
        if 'LIST_BUCKETS' in FLAGS:
            srow("<h2>Listing Buckets</h2>")
            uri = boto.storage_uri('', 'gs')
            buckets = uri.get_all_buckets();
            for bucket in buckets:
                self.response.out.write(bucket.name + '<br />')
        if 'LIST_CONTENTS' in FLAGS:
            srow("<h2>Listing Contents of %s bucket</h2>" % BUCKET_NAME)
            uri = boto.storage_uri(BUCKET_NAME, 'gs')
            objs = uri.get_bucket()
            for obj in objs:
                srow("gs://%s/%s <br />" % (BUCKET_NAME,obj.name))
        if 'FILE' in FLAGS:
            dst_uri = boto.storage_uri(
                BUCKET_NAME + '/' + 'filename.txt', 'gs')
            dst_uri.new_key().set_contents_from_string("contents")
            srow('Successfully created "%s/%s"' % (
                dst_uri.bucket_name, dst_uri.object_name))

ROUTES = [
   ('/storage', MainHandler)
]

application = webapp.WSGIApplication(ROUTES, debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
